﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Entity;
using GoodMan.Infrastructure.Entities;
using System.Data.SqlClient;
using System.Data;

namespace GoodMan.Portal.Biz
{
    public class GroupBuyProductBiz
    {
        public static ItemListBox<Product> GetGroupBuyProducts(string productkey, int areaCode, int categoryId, int subcategoryId, int page, int pagesize)
        {
            var count = new Product().Count(c =>c.AreaCode==areaCode&& (c.ProductType == categoryId || categoryId == 0) && (c.ProductSubType == subcategoryId || subcategoryId == 0) && (c.ProductName.Contains(productkey) || c.ProductCode.Contains(productkey)));
            using (Context ctx = new Context())
            {
                string sql = @"select top (@pagesize) t.* from (select ROW_NUMBER() OVER (ORDER BY a.createdate desc) AS RowNumber,a.*,b.remaincount,d.price,d.StartDate,d.ExpireDate,f.CategoryName,g.CategoryName as SubCategoryName
                   from T_Product a
                    inner join (select ProductId,SUM(Remain) as remaincount from dbo.T_ProductRepertory group by ProductId) b
                    on a.Id=b.ProductId
                    inner join (select * from dbo.T_Policy c where c.PolicyType=1) d 
                    on a.Id=d.ProductId
                    left join dbo.T_ProductCategory f on a.ProductType=f.Id
                    left join dbo.T_ProductCategory g on a.ProductSubType=g.Id
                    where a.AreaCode=@areaCode and (a.ProductType=@categoryId or @categoryId=0) and (a.ProductSubType=@subcategoryId or @subcategoryId=0) and (a.ProductName like '%'+@productkey+'%' or a.ProductCode like '%'+@productkey+'%')) t
                    where t.RowNumber>@pagesize*@page";

                var datatable = ctx.ExcuteSQL(sql,
                    new SqlParameter("@categoryId", categoryId),
                     new SqlParameter("@subcategoryId", subcategoryId),
                     new SqlParameter("@productkey", productkey),
                      new SqlParameter("@areaCode", areaCode),
                      new SqlParameter("@pagesize", pagesize),
                     new SqlParameter("@page", page)
                    );

                List<Product> data = null;
                if (datatable != null && datatable.Rows.Count > 0)
                {
                    data = new List<Product>();
                    foreach (DataRow row in datatable.Rows)
                    {
                        var product = new Product()
                        {
                            CreateDate = Convert.ToDateTime(row["CreateDate"].ToString()),
                            Id = int.Parse(row["Id"].ToString()),
                            Images = (row["Images"].ToString() != "" ? row["Images"].ToString().Split(';')[0] : ""),
                            ProductCode = row["ProductCode"].ToString(),
                            ProductName = row["ProductName"].ToString(),
                            PurchasePrice = decimal.Parse(row["PurchasePrice"] != null ? row["PurchasePrice"].ToString() : "0"),
                            TotalRemain = int.Parse(row["remaincount"] != null ? row["remaincount"].ToString() : "0"),
                            DefaulSalePrice = decimal.Parse(row["Price"] != null ? row["Price"].ToString() : "0"),
                            OriginalPrice = decimal.Parse(row["OriginalPrice"] != null ? row["OriginalPrice"].ToString() : "0"),
                            CapsPeople = int.Parse(row["CapsPeople"] != null ? row["CapsPeople"].ToString() : "0"),
                            CategoryName = row["CategoryName"].ToString(),
                            SubCategoryName = row["SubCategoryName"].ToString(),
                            StartDate = row["StartDate"].ToString(),
                            EndDate = row["ExpireDate"].ToString()
                        };
                        //product.Integral = Convert.ToInt32(Math.Round(product.DefaulSalePrice * 100, 0));
                        data.Add(product);
                    }
                }
                var list = new ItemListBox<Product>(data).BuildPage(
                    count, page, pagesize, new PageParameter() { Style = "workspace" });
                return list;
            }

        }
    }
}
